package com.qiujie.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiujie.entity.StaffLeave;
import com.qiujie.vo.StaffLeaveVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;


public interface StaffLeaveMapper extends BaseMapper<StaffLeave> {


    // StaffLeaveMapper.java

    // 修正分页查询方法
    @Select("SELECT * FROM att_staff_leave WHERE is_deleted = 0 AND staff_id = #{staffId}")
    IPage<StaffLeave> listStaffLeaveByStaffId(
            @Param("page") IPage<StaffLeave> page,  // 必须标注@Param("page")
            @Param("staffId") Integer staffId
    );

    // 重构列表查询方法
    @Select("<script>" +
            "SELECT asl.*, ss.code, ss.name, ss.phone, sd.name dept_name " +
            "FROM att_staff_leave asl " +
            "INNER JOIN sys_staff ss ON asl.staff_id = ss.id " +
            "INNER JOIN sys_dept sd ON ss.dept_id = sd.id " +
            "WHERE asl.is_deleted = 0 AND asl.status IN (0,4) " +
            "<if test='name != null and name != \"\"'>" +
            "   AND ss.name LIKE CONCAT('%',#{name},'%') " +
            "</if>" +
            "<if test='deptId != null'>" +
            "   AND ss.dept_id = #{deptId} " +
            "</if>" +
            "<if test='roles != null and !roles.isEmpty()'>" +
            "   AND EXISTS (" +
            "       SELECT 1 FROM sys_staff_role sr " +
            "       WHERE sr.staff_id = ss.id " +
            "       AND sr.role_code IN " +
            "       <foreach collection='roles' item='role' open='(' separator=',' close=')'>" +
            "           #{role}" +
            "       </foreach>" +
            "   )" +
            "</if>" +
            "</script>")
    IPage<StaffLeaveVO> listStaffLeaveVO(
            @Param("page") IPage<StaffLeaveVO> page,
            @Param("name") String name,
            @Param("deptId") Integer deptId,
            @Param("roles") Set<String> roles  // 修改为角色代码集合
    );

    // StaffMapper.java 新增方法
    @Select("SELECT l.* FROM att_staff_leave l " +
            "WHERE l.staff_id = #{staffId} " +
            "AND l.is_deleted = 0 " +
            "ORDER BY l.create_time DESC")
    IPage<StaffLeave> listByStaffId(Page<StaffLeave> page, @Param("staffId") Integer staffId);


    @Select("<script>" +
            "SELECT " +
            "  l.*, " +
            "  s.name as name, " + // ✅ 映射到StaffLeaveVO.name
            "  d.name as deptName " + // ✅ 映射到StaffLeaveVO.deptName
            "FROM att_staff_leave l " +
            "JOIN sys_staff s ON l.staff_id = s.id " +
            "JOIN sys_dept d ON s.dept_id = d.id " +
            "WHERE l.status = #{status} " +
            "AND l.is_deleted = 0 " +
            "AND l.current_node IN " +
            "<foreach collection='roles' item='role' open='(' close=')' separator=','> " +
            "   (SELECT f.node_order FROM att_approve_flow f " +
            "    WHERE f.flow_key = l.flow_type AND f.role_code = #{role})" +
            "</foreach>" +
            "ORDER BY l.create_time DESC " +
            "LIMIT #{page.size} OFFSET #{page.offset} " +
            "</script>")
    IPage<StaffLeaveVO> selectPendingList(
            @Param("page") IPage<StaffLeaveVO> page,
            @Param("roles") Set<String> roles,
            @Param("status") Integer status
    );



}
